package com.devsoft.foodflip;

import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.BitmapFactory;

public class Game {
	private Player chef;
	private ArrayList <Food> food_list;
	private ArrayList <Food> foods;
	private int foods_inplay;
	private int max_foods;
	private ArrayList <PowerUp> power_list;
	private PowerUp power;
	private int powers_inplay;
	private int max_powers;
	private Map text_map;
	private ArrayList <Unlockable> achievements_list;
	private Timer power_time;
	private Context context;
	
	public Game(boolean game_mode){
		chef = new Player(0,3,BitmapFactory.decodeResource(context.getResources(), R.drawable.panda_chef_with_pan_left_v1));
		food_list = new Food().generateFood();
		power_list = new PowerUp().generatePowerUps();
		achievements_list = new Unlockable().generateAchievements();
		text_map = new Map();
		max_powers=1;
		if(game_mode){
			max_foods=15;
			achievements_list.get(6).increaseCounter();
			achievements_list.get(7).increaseCounter();
			achievements_list.get(8).increaseCounter();
			}else{
				max_foods=5;
				achievements_list.get(0).increaseCounter();
				achievements_list.get(1).increaseCounter();
				achievements_list.get(2).increaseCounter();
				}
		achievements_list.get(12).increaseCounter();
		achievements_list.get(13).increaseCounter();
		achievements_list.get(14).increaseCounter();
		foods = new ArrayList<Food>();
		foods_inplay=0;
		powers_inplay=0;
	}
	
	public void updatePlayer(int new_pos){
		chef.setPosition(new_pos);
        text_map.movePlayer(chef.getPosition(),chef.getPan(),chef.getPowerUpState());
	}
	
	public void rotatePan(){
		if(chef.getPan()){
			chef.setPan(false);
		}else{
			chef.setPan(true);
		}
	}
	
	public void spawnFood(){
		Random rand = new Random();
		if(foods_inplay<max_foods && rand.nextInt(25)==0){
			foods.add(food_list.get(rand.nextInt(18)));//Add a random food
			foods.get(foods_inplay).setPositionX((rand.nextInt(6))*3);//Drop in a random position
			foods.get(foods_inplay).setPositionY(0);//Start at the top
			text_map.setChar(foods.get(foods_inplay).getPositionY(),foods.get(foods_inplay).getPositionX(),'o');
			foods_inplay++;
		}
	}
	
	public void spawnPowerUp(){
		Random rand = new Random();
		if(powers_inplay<max_powers && rand.nextInt(75)==0){
			power = power_list.get(rand.nextInt(4));//Add a random power-up
			power.setPositionX((rand.nextInt(6))*3);//Drop in a random position
			power.setPositionY(0);//Start at the top
			text_map.setChar(power.getPositionY(),power.getPositionX(),'*');
			powers_inplay++;
		}
	}
	
	public void updateFood(){
		for(int i=0;i<foods_inplay;i++){
			//Food is falling
			if(!foods.get(i).getBounce()){
				if(text_map.getChar(foods.get(i).getPositionY()+1,foods.get(i).getPositionX())==' '){
					text_map.setChar(foods.get(i).getPositionY()+1,foods.get(i).getPositionX(),'o');
					text_map.setChar(foods.get(i).getPositionY(),foods.get(i).getPositionX(),' ');
					foods.get(i).setPositionY(foods.get(i).getPositionY()+1);
	                }
				else if(foods.get(i).getPositionY()==15){
					text_map.setChar(15,foods.get(i).getPositionX(),' ');
					foods = new Food().removeFood(foods, foods.get(i));
	                foods_inplay--;
	                }
				else if(text_map.getChar(foods.get(i).getPositionY()+1,foods.get(i).getPositionX())=='x'){
					text_map.setChar(foods.get(i).getPositionY(),foods.get(i).getPositionX(),' ');
	                this.hitFood(foods.get(i));
	                foods_inplay--;;
	                }
			}
			//Food is going up
			else{
				if(text_map.getChar(foods.get(i).getPositionY()-1,foods.get(i).getPositionX())==' '){
					text_map.setChar(foods.get(i).getPositionY()-1,foods.get(i).getPositionX(),'o');
					text_map.setChar(foods.get(i).getPositionY(),foods.get(i).getPositionX(),' ');
					foods.get(i).setPositionY(foods.get(i).getPositionY()-1);
	                }
				else if(foods.get(i).getPositionY()==0){
	                foods.get(i).setBounce(false);
	                }
			}
		}
	}
	
	public void updatePowerUp(){
		if(!power_time.running()){//Power-up time is over
			chef.setPowerUpState(0);
		}
		if(powers_inplay==1){
			//Power-up is falling
			if(text_map.getChar(power.getPositionY()+1,power.getPositionX())==' '){
				text_map.setChar(power.getPositionY()+1,power.getPositionX(),'*');
				text_map.setChar(power.getPositionY(),power.getPositionX(),' ');
				power.setPositionY(power.getPositionY()+1);
                }
			//Power-up hits the floor
			else if(power.getPositionY()==15){
				text_map.setChar(15,power.getPositionX(),' ');
                powers_inplay=0;
                }
			//Power-up hit the pan
			else if(text_map.getChar(power.getPositionY()+1,power.getPositionX())=='x'){
				text_map.setChar(power.getPositionY(),power.getPositionX(),' ');
                this.hitPowerUp(power.getName());
                powers_inplay=0;
                }
		}
	}
	
	public void hitFood(Food hit){
		int points=0;
		//Is food cooked?
		if(hit.getFullyCooked()){
			foods = new Food().removeFood(foods, hit);
			foods_inplay--;
			points=10;
			achievements_list.get(18).increaseCounter();
			achievements_list.get(19).increaseCounter();
			achievements_list.get(20).increaseCounter();
		}else{
			hit.setBounce(true);
			hit.incrementCooked(hit.getCooked());
			points=1;
			achievements_list.get(21).increaseCounter();
			achievements_list.get(22).increaseCounter();
			achievements_list.get(23).increaseCounter();
		}
		//Increase player's score
		if(chef.getPowerUpState()!=2){//Score Multiplier power-up
			chef.setScore(chef.getScore()+points);
		}else{
			chef.setScore(chef.getScore()+(points*2));
		}
	}
	
	public void hitPowerUp(String power){
		power_time = new Timer(30000);
		power_time.start();
		if(power=="Double Pan"){
			chef.setPowerUpState(1);
		}
		else if(power=="Score Multiplier"){
			chef.setPowerUpState(2);
		}
		else if(power=="Speed Boost"){
			chef.setPowerUpState(3);
		}
		else if(power=="Slow Time"){
			chef.setPowerUpState(4);
		}
		achievements_list.get(15).increaseCounter();
		achievements_list.get(16).increaseCounter();
		achievements_list.get(17).increaseCounter();
	}
}
